package com.example.chao.myphotosearch.view.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.example.chao.myphotosearch.R;
import com.example.chao.myphotosearch.adapters.SearchAdapter;
import com.example.chao.myphotosearch.bean.SosoImage;
import com.example.chao.myphotosearch.model.DaggerLaunchComponent;
import com.example.chao.myphotosearch.presenter.SearchFragmentPresenter;
import com.example.chao.myphotosearch.view.IView;
import com.example.chao.myphotosearch.view.activities.ShowLargeImgActivity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by chao on 2016/4/7.
 */
public class SearchFragment extends Fragment implements IView<List<SosoImage>> ,SearchAdapter.OnSearchItemClickListener{
    @Bind(R.id.search_recycler_view)
    RecyclerView searchRecyclerView;
    @Inject
    SearchAdapter searchAdapter;
    @Inject
    SearchFragmentPresenter searchPresenter;
    @Bind(R.id.search_fab)
    FloatingActionButton searchFab;

    private int lastYposition = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        DaggerLaunchComponent.create().inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_search, container, false);
        ButterKnife.bind(this, root);
        return root;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        searchPresenter.onCreate(this);
        initSearchRecyclerView();
    }

    @OnClick(R.id.search_fab)
    public void onFabClick(View view){
        searchRecyclerView.scrollToPosition(0);
    }

    private void initSearchRecyclerView() {
        final StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        searchRecyclerView.setLayoutManager(layoutManager);
        searchAdapter.setListener(this);
        searchRecyclerView.setAdapter(searchAdapter);
        searchRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int totalCount = layoutManager.getItemCount();
                int[] visibleItems = layoutManager.findLastVisibleItemPositions(null);
                int lastItem = Math.max(visibleItems[0], visibleItems[1]);
                if (dy > 0 && lastItem > totalCount - 2) {
                    searchPresenter.loadMore();
                }
            }
        });
        searchRecyclerView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        lastYposition = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        if (lastYposition - event.getY() < 0) {
                            searchFab.show();
                        } else {
                            searchFab.hide();
                        }
                        lastYposition = (int) event.getY();
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void attachView(List<SosoImage> data) {
        searchAdapter.setSosoImages(data);
    }

    @Override
    public void detachView() {

    }

    @Override
    public void onSearchItemClick(int position, ArrayList<SosoImage> list) {
        Intent intent = new Intent();
        intent.putExtra("position", position);
        intent.putExtra("netImages", list);
        intent.setClass(getView().getContext(), ShowLargeImgActivity.class);
        startActivityForResult(intent,100);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==100){
            searchRecyclerView.scrollToPosition(data.getIntExtra("position",0));
        }
    }
}
